package com.hua.controller;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hua.entities.Category;
import com.hua.entities.R;
import com.hua.service.serviceImpl.CategoryServiceImpl;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryServiceImpl categoryService;
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        System.out.println(category);
        categoryService.save(category);
        return R.success("增加分类成功~~~~");
    }

    @GetMapping("/page")
    public R<Page> getListOfCategory(int page, int pageSize){
        Page<Category> getPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(getPage,queryWrapper);
        return R.success(getPage);
    }

    @DeleteMapping
    public R<String> deleteById(@RequestParam("ids")Long ids){
        categoryService.removeById(ids);
        return R.success("删除成功~~~~");
    }

    @PutMapping
    public R<Category> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success(category);
    }

    @GetMapping("/list")
    public R<List<Category>> getType(Category category){
        LambdaQueryWrapper <Category>lambdaQueryWrapper
                = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByAsc(Category::getUpdateTime);

        List<Category> list = categoryService.list(lambdaQueryWrapper);
        list.forEach(System.out::println);
        return R.success(list);
    }
}
